//
//  LSCoverUserInfo.m
//  HappyStore
//
//  Created by 陈锋 on 16/8/15.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "LSCoverUserInfo.h"
#import "UIResponder+LSCustHUB.h"

@implementation LSCoverUserInfo

+(void)CoverUserInfo{
    
    NSMutableDictionary *muDicParameters = [NSMutableDictionary dictionary];
    
    NSString * urlStr;
    
    if ([USER_DEFAULTS objectForKey:@"threeKey"]) {
        
        [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"threeKey"] forKey:@"threeKey"];
        [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"threeUserName"] forKey:@"userName"];
        [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"threeHeadPhoto"] forKey:@"headPhoto"];
        urlStr = @"threeLogin_app_user.im";
        
    }else{
    
    [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"account"] forKey:@"login_account"];//登录用户名
    [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"password"] forKey:@"password"];//密码
        urlStr = @"login_app_user.im";
        
    }
    
    NSString *strURl = [NSString stringWithFormat:@"%@%@",URL_DEFINE,urlStr];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:strURl parameters:muDicParameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
//        NSLog(@"%@",dicInfo);
        

        if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
            //"data":[{"phone":"13896438419","email":"","token":"SX8QbfVIl6RAXqwjgUn8","userid":9,"account":"cxs123","usertype":1,"password":"e10adc3949ba59abbe56e057f20f883e"}]
            NSDictionary *dicUserInfo = [dicInfo objectForKey:@"data"][0];
       
            allConstMemberid = dicUserInfo[@"userid"];
            allConstMemberToken = dicUserInfo[@"token"];
            
            if ([USER_DEFAULTS objectForKey:@"threeKey"]) {
                
                [USER_DEFAULTS setObject:muDicParameters[@"userName"] forKey:@"threeUserName"];
                [USER_DEFAULTS setObject:muDicParameters[@"headPhoto"] forKey:@"threeHeadPhoto"];
                [USER_DEFAULTS setObject:muDicParameters[@"threeKey"] forKey:@"threeKey"];
                
            }
            
            [USER_DEFAULTS setObject:dicUserInfo[@"token"] forKey:@"token"];
            [USER_DEFAULTS setObject:dicUserInfo[@"phone"] forKey:@"phone"];
            [USER_DEFAULTS setObject:dicUserInfo[@"userid"] forKey:@"userid"];
            [USER_DEFAULTS setObject:dicUserInfo[@"account"] forKey:@"account"];
            [USER_DEFAULTS setObject:[NSString stringWithFormat:@"%@",dicUserInfo[@"usertype"]] forKey:@"usertype"];
            [USER_DEFAULTS setObject:[muDicParameters objectForKey:@"password"] forKey:@"password"];
            [USER_DEFAULTS setObject:dicUserInfo[@"industry"] forKey:@"industry"];
            [USER_DEFAULTS setObject:dicUserInfo[@"address"] forKey:@"address"];
            [USER_DEFAULTS setObject:dicUserInfo[@"imToken"] forKey:@"imToken"];
            [USER_DEFAULTS setObject:dicUserInfo[@"userName"] forKey:@"userName"];
            [USER_DEFAULTS setObject:dicUserInfo[@"headPhoto"] forKey:@"headPhoto"];
            [USER_DEFAULTS setObject:dicUserInfo[@"vipLevel"] forKey:@"vipLevel"];
            
            NSString *key = @"CFBundleVersion";
            // 当前软件的版本号（从Info.plist中获得）
            NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
            // 将当前的版本号存进沙盒
            [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            [USER_DEFAULTS synchronize];
            
            [self CoverVipType];
            
            
        }else {
            [MRProgressOverlayView showErrorWithStatus:@"请输入正确的账号密码或者请您注册"];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MRProgressOverlayView showErrorWithStatus:error.localizedDescription];
        
    }];


}

+(void)CoverVipType{

    [LSHttpTool sendHttpData:@"hideAlipay_public.htm" params:nil success:^(id responseObject) {
        
         [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
        
        [MagicalRecord saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) {
            
            Person * tmp = [Person MR_findFirstByAttribute:@"userid" withValue:allConstMemberid inContext:localContext];
            
            tmp.vipType = [NSString stringWithFormat:@"%@",responseObject[@"data"]];
            
        }
                          completion:^(BOOL contextDidSave, NSError *error) {
                              NSLog(@"=-===%@", (contextDidSave ? @"saveSuccessed" : @"saveFailure"));
                              
                          }];
        
        [USER_DEFAULTS removeObjectForKey:@"vipType"];
        [USER_DEFAULTS setObject:responseObject[@"data"] forKey:@"vipType"];
        [USER_DEFAULTS synchronize];
        NSLog(@"我是会员状态:%@",[USER_DEFAULTS objectForKey:@"vipType"]);
    } failure:^(NSError *error) {
        NSLog(@"会员状态错误:%@",error);
    }];

}
@end
